define([
    'jquery',
    'require',
    'bigwe_utils',
], function($, require) {
    /* ========== * 定时发送列表 * ========== */
    var returnParam = {};
    var unique1 = function(arrayObj) {
        var n = []; //一个新的临时数组
        for (var i = 0; i < arrayObj.length; i++) //遍历当前数组
        {
            //如果当前数组的第i已经保存进了临时数组，那么跳过，
            //否则把当前项push到临时数组里面
            if (n.indexOf(arrayObj[i]) == -1) n.push(arrayObj[i]);
        }
        return n;
    }

    var timed = {
        //信息类型
        msgType: {
            mpnews: '【图文】',
            text: '【文字】',
            image: '【图片】',
            video: '【视频】',
            voice: '【语音】',
        },
        //信息状态
        msgStatus: {
            1: '发送成功',
            2: '待发送',
            3: '发送失败',
            4: '已删除',
            10: '发送中',
            11: '发送异常',
            16: '已发送'
        },
        //信息状态图标
        msgStatusIcon: {
            1: 'icon-gouxuan green',
            2: 'icon-fangxiang',
            3: 'icon-gantanhao red',
            4: '已删除',
            10: 'icon-hulve',
            11: '发送异常',
            16: '已发送'
        },
        //信息发送类型
        msgSendType: {
            1: '定时发送',
            0: '立即发送'
        }
    }

    returnParam.init = function(json) {
        var data = json.data,
            tempArray = [],
            trip_html = '',
            msg = '',
            ico = '',
            ico_tit = '',
            today = true;

        // yyyy-MM-dd hh:mm:ss
        if (json.pt == 'weixin') {
            if (data.ret == 0) {
                var _json = data.data.list.reverse();
                for (var i in _json) {
                    var send_cont = _json[i],
                        _time = new Date(send_cont.appointment_time);
                    tempArray.push(_time.format('yyyy-MM-dd'));
                    if (_time.format('yyyy-MM-dd') == json.time) {
                        today = false;
                    }
                    var js_app = typeof send_cont.js_app === 'string' ? $.parseJSON(send_cont.js_app) : send_cont.js_app;
                    var user_group = (js_app.app_user_group == '' || js_app.app_user_group == undefined || js_app.app_user_group == null) ? '全部用户' : (js_app.app_user_group);
                    var js_content = typeof send_cont.js_content === 'string' ? $.parseJSON(send_cont.js_content) : send_cont.js_content;

                    trip_html += '<div class="result"><p>' + _time.format('MM月dd日') + '</p><div class="sub_info" data-timing-id="' + send_cont.id + '" data-timing-cd="' + send_cont.res_id + '" data-timing-time="' + send_cont.appointment_time + '" data-send-type="' + send_cont.msg_type_cd + '"><span class="info_time">' + _time.format('hh:mm') + '</span><span class="info_group" data-group="' + ((user_group == '全部用户') ? '' : user_group) + '">' + user_group + '</span><span class="iconfont ' + timed.msgStatusIcon[send_cont.msg_status_cd] + '" data-toggle="tooltip" data-original-title="' + timed.msgStatus[send_cont.msg_status_cd] + '"></span></span><p>' + timed.msgType[send_cont.msg_type_cd] + (js_content.texts[0].title || '') + '</p></div></div>';
                }
            }
        } else if (json.pt == 'sina') {
            if (data.ret == 0) {
                if ($('#send_destina').val() == 'send_letter') {
                    for (var i in data.data) {
                        var send_cont = data.data[i],
                            _time = new Date(send_cont.send_time);
                        tempArray.push(_time.format('yyyy-MM-dd'));
                        if (_time.format('yyyy-MM-dd') == json.time) {
                            today = false;
                        }

                        //判断类型
                        if (send_cont.msgtype == 'text') {
                            wenz_nr = timed.msgType[send_cont.msgtype] + (send_cont.text || '');
                        } else if (send_cont.msgtype == 'mpnews') {
                            wenz_nr = timed.msgType[send_cont.msgtype] + (send_cont.article_group_title || '');
                        }
                        if (send_cont.send_type == '2') {
                            ico = 'icon-gouxuan green';
                            ico_tit = '发送成功';
                        } else if (send_cont.send_type == '4') {
                            ico = 'icon-fangxiang';
                            ico_tit = '待发送';
                        } else if (send_cont.send_type == '3') {
                            ico = 'icon-gantanhao red';
                            ico_tit = '发送失败';
                        } else {
                            ico = 'icon-hulve';
                            ico_tit = '发送中';
                        }

                        trip_html += '<div class="result"><p>' + _time.format('MM月dd日') + '</p><div class="sub_info" data-timing-id="' + send_cont.id + '" data-timing-cd="' + send_cont.article_group_cd + '" data-timing-time="' + send_cont.send_time + '" data-send-type="' + send_cont.msgtype + '"><span class="info_time">' + _time.format('hh:mm') + '</span><span class="info_group" data-group="' + send_cont.group_id + '">' + send_cont.group_name + '</span><span class="iconfont ' + ico + '" data-toggle="tooltip" data-original-title="' + ico_tit + '"></span></span><p>' + wenz_nr + '</p></div></div>';
                    }
                } else {
                    for (var i in data.data) {
                        var send_cont = data.data[i];
                        tempArray.push(i);
                        if (i == json.time) {
                            today = false;
                        }

                        trip_html += '<div class="result' + ((send_cont.length > 1) ? ' result_many' : '') + '"><p>' + new Date(i).format('MM月dd日') + '<a herf="javascript:void(0)" class="pull-right add_timed" data-time="' + i + '">新建分发</a></p>';

                        var len;
                        if (send_cont.length > 2) {
                            len = (send_cont.length % 2 != 0) ? (send_cont.length - send_cont.length % 2) : (send_cont.length - 2);
                        }

                        for (var j in send_cont) {
                            if (send_cont[j].send_type == '2') {
                                ico = 'icon-gouxuan green';
                                ico_tit = '发送成功';
                            } else if (send_cont[j].send_type == '4') {
                                ico = 'icon-fangxiang';
                                ico_tit = '待发送';
                            } else if (send_cont[j].send_type == '3') {
                                ico = 'icon-gantanhao red';
                                ico_tit = '发送失败';
                            } else {
                                ico = 'icon-hulve';
                                ico_tit = '发送中';
                            }

                            trip_html += '<div class="sub_info' + ((len && j < len) ? ' sub_info_b' : '') + '" data-timing-id="' + send_cont[j].id + '" data-timing-cd="' + send_cont[j].image + '" data-timing-time="' + send_cont[j].send_time + '" data-send-type="' + send_cont[j].type + '"><span class="info_time">' + new Date(send_cont[j].send_time).format('hh:mm') + '</span><span class="info_group" data-group="0">公开</span><span class="iconfont ' + ico + '" data-toggle="tooltip" data-original-title="' + ico_tit + '"></span></span><p>' + timed.msgType[send_cont[j].type] + (send_cont[j].text || '') + '</p></div>';
                        }
                        trip_html += '</div>';
                    }
                }
            }
        } else if (json.pt == 'toutiao') {
            if (data.data) {
                for (var i in data.data) {
                    var send_cont = data.data[i],
                        _time = new Date(send_cont.send_time),
                        _title;
                    tempArray.push(_time.format('yyyy-MM-dd'));
                    if (_time.format('yyyy-MM-dd') == json.time) {
                        today = false;
                    }
                    //判断类型
                    if (send_cont.send_type == '2') {
                        ico = 'icon-gouxuan green';
                        ico_tit = '发送成功';
                    } else if (send_cont.send_type == '1') {
                        ico = 'icon-fangxiang';
                        ico_tit = '待发送';
                    } else if (send_cont.send_type == '3') {
                        ico = 'icon-gantanhao red';
                        ico_tit = '发送失败';
                    } else {
                        ico = 'icon-hulve';
                        ico_tit = '发送中';
                    }

                    if (send_cont.type == 'mpnews') {
                        _title = '【图文】'
                    } else {
                        _title = '【视频】'
                    }

                    trip_html += '<div class="result"><p>' + _time.format('MM月dd日') + '</p><div class="sub_info" data-timing-id="' + send_cont.id + '" data-timing-cd="' + (send_cont.content || send_cont.group_cd) + '" data-timing-time="' + send_cont.send_time + '" data-send-type="' + send_cont.type + '"><span class="info_time">' + _time.format('hh:mm') + '</span><span class="info_group" data-group=""></span><span class="iconfont ' + ico + '" data-toggle="tooltip" data-original-title="' + ico_tit + '"></span></span><p>' + _title + (send_cont.title || '') + '</p></div></div>';
                }
            }
        }
        if (today) {
            msg = '<div class="result"><p>' + new Date(json.time).format('MM月dd日') + '<a herf="javascript:void(0)" class="pull-right add_timed" data-time="' + json.time + '">新建分发</a></p><p class="empty">今日无分发消息</p></div>';
        }

        $(".trip_content").empty().append(msg + trip_html);
        if (json.callKBack) {
            tempArray = unique1(tempArray);
            var rt_arr = [];
            for (var key in tempArray) {
                rt_arr.push({
                    'start': new Date(tempArray[key]),
                    id: 'sign' + key
                });
            }
            json.callKBack.call(this, rt_arr);
        }
        $('[data-toggle="tooltip"]').tooltip({
            trigger: 'hover'
        });
    }

    var pack = $('.mass_timing');
    $('.trip_content').on('click', '.add_timed', function() {
        $('.mass_button').find('.delete_plan').addClass('hide').removeAttr('data-id').data('id', false);
        $('.mass_button').find('.send_out').removeAttr('data-modify').data('modify', false);

        var timing_time = new Date($(this).data('time'));
        if (timing_time) {
            var fcs_month = pack.find('#fcs_date_month').val() * 1 + 1;
            (fcs_month < 10) ? (fcs_month = '0' + fcs_month) : fcs_month;
            if (timing_time.format('MM') != fcs_month) {
                pack.find('.trip_calendar').fullCalendar('gotoDate', timing_time.format('yyyy'), timing_time.format('MM') * 1 - 1, timing_time.format('dd'))
            }

            pack.find(".fc-state-highlight").removeClass("fc-state-highlight");
            $("[data-date=" + timing_time.format('yyyy-MM-dd') + "]").addClass("fc-state-highlight");
            $("[date=" + timing_time.format('yyyy-MM-dd') + "]").css({
                backgroundColor: 'white'
            });
        }
        $('html,body').scrollTop(document.body.clientHeight);
    })

    return returnParam;
});
